package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsFlashPromotion;
import com.itheima.domain.SmsHomeBrand;
import com.itheima.mapper.SmsHomeBrandMapper;
import com.itheima.service.db.SmsHomeBrandService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsHomeBrandServiceImpl implements SmsHomeBrandService {

    @Autowired
    SmsHomeBrandMapper smsHomeBrandMapper;

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize,String brandName,Integer recommendStatus) {
        Page<SmsHomeBrand> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsHomeBrand> qw = new LambdaQueryWrapper<>();

        qw.like(StrUtil.isNotEmpty(brandName),SmsHomeBrand::getBrandName,brandName);
        qw.eq(recommendStatus != null,SmsHomeBrand::getRecommendStatus,recommendStatus);
        qw.orderByDesc(SmsHomeBrand::getSort);

        Page<SmsHomeBrand> smsHomeBrandPage = smsHomeBrandMapper.selectPage(page, qw);

        return new PageBeanVo(pageNum,pageSize,smsHomeBrandPage.getTotal(),smsHomeBrandPage.getRecords());
    }

    @Override
    public void updateRecommendStatus(Long id, Integer recommendStatus) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setRecommendStatus(recommendStatus);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }
}
